const jwt = require('jsonwebtoken')
const mysecret = 'mysecret'
const Token = async (req) => {
    const token = await jwt.sign({ _id: req.body.username }, mysecret ,{  expiresIn: '1h'});
    console.log(token,'颁发令牌');
    return token
};
const checkToken = async (req) => {
    console.log(req.request.header.token);
    const token = JSON.parse(req.request.header.token)
    const decode = await jwt.verify(token, mysecret, function (err, decoded) {
        if (err) {
            console.log('无效令牌');
            return {
                code: 202,
                msg: "无效令牌"
            }
        }else{
            console.log(decoded,'令牌有效');
        }

    });
    console.log(decode, 'decode');

    return decode
}


module.exports = {
    Token,
    mysecret,
    checkToken
}